//
//  WZGCanWithDrawInputVC.m
//  ZCJZ
//
//  Created by sujeking on 2019/8/19.
//  Copyright © 2019 jeking. All rights reserved.
//

#import "WZGOrderPayWechatAlertVC.h"
#import "WZGOrderPayWechatAlertTopView.h"
#import "WZGOrderPayWechatAlertBottomView.h"
#import "ZCOrderCheckLeftAndRightCell.h"
#import "WZGPayTypeSelectCell.h"

@interface WZGOrderPayWechatAlertVC ()

@property (weak, nonatomic) IBOutlet UIView *bgview;
@property (weak, nonatomic) IBOutlet UILabel *canwithdrawlabel;
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;

@property (weak, nonatomic) IBOutlet UILabel *pricelabel;
@property (weak, nonatomic) IBOutlet UIImageView *leftimv;
@property (weak, nonatomic) IBOutlet UIImageView *rightimv;

@property (weak, nonatomic) IBOutlet UIButton *leftbtn;
@property (weak, nonatomic) IBOutlet UIButton *rightbtn;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic) NSInteger actionType;
@property (assign, nonatomic) NSInteger selectType;
@property (assign, nonatomic) NSInteger lastRow;

@end

@implementation WZGOrderPayWechatAlertVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    [self.view setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.4]];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.layer.cornerRadius = 5.0f;
    self.tableView.layer.masksToBounds = YES;
    self.selectType = -1;
    self.lastRow = -1;

    
    [self.tableView registerNib:[UINib nibWithNibName:@"WZGPayTypeSelectCell" bundle:nil]
         forCellReuseIdentifier:@"WZGPayTypeSelectCell"];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismissViewControllerAnimated:YES completion:nil];
}

// MARK: - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.payType.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    WZGPayTypeSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WZGPayTypeSelectCell"
                                                                 forIndexPath:indexPath];
    NSDictionary *dict = self.payType[row];
    cell.dict = dict;
    cell.pt = [[dict valueForKey:@"id"] stringValue];
    cell.message = [dict valueForKey:@"message"];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UINib *nib = [UINib nibWithNibName:@"WZGOrderPayWechatAlertTopView" bundle:nil];
    WZGOrderPayWechatAlertTopView *view = [nib instantiateWithOwner:self options:nil].firstObject;
    view.priceStr = self.goodsprice;
    [view setBtnClickCallback:^{
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    __weak typeof(self) weakSelf = self;
    UINib *nib = [UINib nibWithNibName:@"WZGOrderPayWechatAlertBottomView" bundle:nil];
    WZGOrderPayWechatAlertBottomView *view = [nib instantiateWithOwner:self options:nil].firstObject;
    if (self.payType.count != 0) {
        view.titles = @[@"充值余额",@"确认支付"];
    } else {
        view.titles = @[@"充值余额"];
    }
    [view setBtnClickCallback:^(NSInteger tag) {
        weakSelf.actionType = tag;
        if (tag == 10) {
            [weakSelf.delegate WZGOrderPayWechatAlertVCWithdraw];
        } else {
            if (weakSelf.selectType == -1) {
                [weakSelf.view makeToast:@"请选择支付方式" duration:2.0 position:CSToastPositionCenter];
                return;
            } else {
                [weakSelf.delegate WZGOrderPayWechatAlertVCPayWithPayType:weakSelf.selectType];
            }
        }
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    }];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 200.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 80.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSDictionary *dict = self.payType[row];
    
    BOOL enable = [[dict valueForKey:@"enable"] boolValue];
    if (enable) {
        self.lastRow = row;
        self.selectType = [[dict valueForKey:@"id"] intValue];
    } else {
        if (self.lastRow == -1) {
            [tableView deselectRowAtIndexPath:indexPath animated:NO];
        } else {
            NSIndexPath *path = [NSIndexPath indexPathForRow:self.lastRow inSection:0];
            [self.tableView selectRowAtIndexPath:path animated:NO scrollPosition:UITableViewScrollPositionNone];
        }
    }
}

@end
